/**
 * 
 */
package com.xijing.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import com.xijing.model.*;
import com.xijing.service.AdminService;
import com.xijing.util.RendedTable;


public class SystemAuthFrame extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private List<SYS_User> admList = new ArrayList<SYS_User>(); 
	private AdminService adminService = new AdminService();
	
	private JPanel jContentPane = null;
	private JScrollPane tableScrollPane = null;
	private RendedTable admListTable = null;
	private String[][] tableData = null;
	private DefaultTableModel model = null;
	private JButton btn_select = null;
	private JTree tree;

	/**
	 * This method initializes
	 * 
	 */
	public SystemAuthFrame() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
		this.setBounds(new Rectangle(300, 80, 600, 400));
		this.setContentPane(getJContentPane());
		this.setTitle("管理员管理界面");
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		display(admList);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getTableScrollPane());
			
			JScrollPane treeScrollPane = new JScrollPane();
			treeScrollPane.setBounds(241, 5, 343, 320);
			jContentPane.add(treeScrollPane);
			treeScrollPane.setColumnHeaderView(getTree());
			treeScrollPane.setAutoscrolls(true);
			jContentPane.add(getBtn_select());
		}
		return jContentPane;
	}

	/**
	 * This method initializes tableScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getTableScrollPane() {
		if (tableScrollPane == null) {
			tableScrollPane = new JScrollPane();
			tableScrollPane.setBounds(5, 5, 226, 320);
			tableScrollPane.setViewportView(getAdmListTable());
		}
		return tableScrollPane;
	}

	/**
	 * This method initializes contractListPanel
	 * 
	 * @return javax.swing.RendedTable
	 */
	private RendedTable getAdmListTable() {
		if (admListTable == null) {
			String[] header = { "管理员编号", "姓名", "状态" };
			model = new DefaultTableModel(tableData, header) {
				private static final long serialVersionUID = 1L;

				public int getColumnCount() {
					return 3;
				}

				public boolean isCellEditable(int row, int column) {
					return false;
				}
			};

			admListTable = new RendedTable(model);
			admListTable.setShowGrid(true);
			admListTable.setSelectionForeground(Color.RED);

			admListTable.setBackground(Color.LIGHT_GRAY);
			admListTable.setMinimumSize(new Dimension(150, 80));
			admListTable.setAutoResizeMode(RendedTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
			admListTable.setBounds(new Rectangle(0, 0, 800, 80));
			admListTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

			admListTable.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					rowClick(e);
				}
			});
			
		}
		return admListTable;
	}

	/**
	 * 
	* @Title: rowClick
	* @Description: TODO 表格鼠标点击事件
	* @param @param e    设定文件
	* @return void    返回类型
	* @throws
	 */
	private void rowClick(MouseEvent e){
		int row = admListTable.getSelectedRow();
		DefaultTableModel dtm = (DefaultTableModel) admListTable.getModel();
		String aid = (String) dtm.getValueAt(row, 0);
		JOptionPane.showMessageDialog(this, aid);
	}
	
	/**
	 * This method initializes btn_select
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtn_select() {
		if (btn_select == null) {
			btn_select = new JButton();
			btn_select.setBounds(5, 335, 93, 26);
			btn_select.setText("查  询");
			btn_select.addActionListener(this);
		}
		return btn_select;
	}

	public void display(List<SYS_User> admList) {
		if (getAdmListTable() != null) {
			// 清空当前表格的所有值
			model.setRowCount(0);
			this.admList = admList;
			tableData = new String[this.admList.size()][model.getColumnCount()];
			for (int i = 0; i < this.admList.size(); i++) {
				tableData[i][0] = String.valueOf(this.admList.get(i).getSU_ID());
				tableData[i][1] = this.admList.get(i).getSU_RealName();
				switch (this.admList.get(i).getSU_State()) {
				case 1:
					tableData[i][2] = "正常";
					break;
				case 2:
					tableData[i][2] = "锁定";
					break;
				case 3:
					tableData[i][2] = "删除";
					break;
				default:
					tableData[i][2] = "-";
				}
				
				model.addRow(tableData[i]);
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		//查询按钮
		if (e.getSource() == this.btn_select) {
			List<SYS_User> admList = adminService.search();
			this.display(admList);
		}
	}

	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SystemAuthFrame frame = new SystemAuthFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	private JTree getTree() {
		if (tree == null) {
			tree = new JTree();
			tree.setModel(new DefaultTreeModel(
				new DefaultMutableTreeNode("宾馆管理系统") {
					{
						DefaultMutableTreeNode node_1;
						node_1 = new DefaultMutableTreeNode("基础设置");
							node_1.add(new DefaultMutableTreeNode("客房信息设置"));
							node_1.add(new DefaultMutableTreeNode("消费物品设置"));
						add(node_1);
						node_1 = new DefaultMutableTreeNode("前台操作");
							node_1.add(new DefaultMutableTreeNode("入住管理"));
							node_1.add(new DefaultMutableTreeNode("增加消费"));
							node_1.add(new DefaultMutableTreeNode("结账退房"));
							node_1.add(new DefaultMutableTreeNode("入住登记"));
							node_1.add(new DefaultMutableTreeNode("单据打印"));
						add(node_1);
						node_1 = new DefaultMutableTreeNode("预定管理");
							node_1.add(new DefaultMutableTreeNode("预订登记"));
							node_1.add(new DefaultMutableTreeNode("确定入住"));
							node_1.add(new DefaultMutableTreeNode("解除预定"));
							node_1.add(new DefaultMutableTreeNode("预定提醒"));
						add(node_1);
						node_1 = new DefaultMutableTreeNode("查询统计");
							node_1.add(new DefaultMutableTreeNode("房间状态"));
							node_1.add(new DefaultMutableTreeNode("历史住宿统计"));
							node_1.add(new DefaultMutableTreeNode("历史收费查询"));
						add(node_1);
					}
				}
			));
			tree.setAutoscrolls(true);
		}
		return tree;
	}
} // @jve:decl-index=0:visual-constraint="10,10"
